﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using Model.Interfaces;
using Model.ModelClasses;
using Model.ModelEventArgs;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.Teacher")]
    public partial class Teacher : AbstractMappingClass, INotifyPropertyChanging, INotifyPropertyChanged
    {
        public event EventHandler<ModelItemEventArgs> AddedSubjectEvent;
        public event EventHandler<ModelItemEventArgs> RemovedSubjectEvent;

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idTeacher;

        private string _familyName;

        private string _teacherName;

        private string _fatherName;

        private Nullable<DateTime> _bornDate;

        private string _email;

        private string _homePhone;

        private string _mobilePhone;

        private string _additionalPhone;

        private Nullable<bool> _IsCandidate;

        private EntitySet<TeacherSubject> _TeacherSubjects;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnidTeacherChanging(short value);
        partial void OnidTeacherChanged();
        partial void OnfamilyNameChanging(string value);
        partial void OnfamilyNameChanged();
        partial void OnteacherNameChanging(string value);
        partial void OnteacherNameChanged();
        partial void OnfatherNameChanging(string value);
        partial void OnfatherNameChanged();
        partial void OnbornDateChanging(System.Nullable<System.DateTime> value);
        partial void OnbornDateChanged();
        partial void OnemailChanging(string value);
        partial void OnemailChanged();
        partial void OnhomePhoneChanging(string value);
        partial void OnhomePhoneChanged();
        partial void OnmobilePhoneChanging(string value);
        partial void OnmobilePhoneChanged();
        partial void OnadditionalPhoneChanging(string value);
        partial void OnadditionalPhoneChanged();
        partial void OnIsCandidateChanging(System.Nullable<bool> value);
        partial void OnIsCandidateChanged();
        #endregion

        public Teacher()
        {
            this._TeacherSubjects = new EntitySet<TeacherSubject>(AttachTeacherSubjects, DetachTeacherSubjects);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idTeacher", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idTeacher
        {
            get
            {
                return this._idTeacher;
            }
            set
            {
                if ((this._idTeacher != value))
                {
                    this.OnidTeacherChanging(value);
                    this.SendPropertyChanging();
                    this._idTeacher = value;
                    this.SendPropertyChanged("idTeacher");
                    this.OnidTeacherChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_familyName", DbType = "VarChar(75) NOT NULL", CanBeNull = false)]
        public string familyName
        {
            get
            {
                return this._familyName;
            }
            set
            {
                if ((this._familyName != value))
                {
                    this.OnfamilyNameChanging(value);
                    this.SendPropertyChanging();
                    this._familyName = value;
                    this.SendPropertyChanged("familyName");
                    this.OnfamilyNameChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_teacherName", DbType = "VarChar(75) NOT NULL", CanBeNull = false)]
        public string teacherName
        {
            get
            {
                return this._teacherName;
            }
            set
            {
                if ((this._teacherName != value))
                {
                    this.OnteacherNameChanging(value);
                    this.SendPropertyChanging();
                    this._teacherName = value;
                    this.SendPropertyChanged("teacherName");
                    this.OnteacherNameChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_fatherName", DbType = "VarChar(75) NOT NULL", CanBeNull = false)]
        public string fatherName
        {
            get
            {
                return this._fatherName;
            }
            set
            {
                if ((this._fatherName != value))
                {
                    this.OnfatherNameChanging(value);
                    this.SendPropertyChanging();
                    this._fatherName = value;
                    this.SendPropertyChanged("fatherName");
                    this.OnfatherNameChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_bornDate", DbType = "DateTime")]
        public System.Nullable<System.DateTime> bornDate
        {
            get
            {
                return this._bornDate;
            }
            set
            {
                if ((this._bornDate != value))
                {
                    this.OnbornDateChanging(value);
                    this.SendPropertyChanging();
                    this._bornDate = value;
                    this.SendPropertyChanged("bornDate");
                    this.OnbornDateChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_email", DbType = "VarChar(100)")]
        public string email
        {
            get
            {
                return this._email;
            }
            set
            {
                if ((this._email != value))
                {
                    this.OnemailChanging(value);
                    this.SendPropertyChanging();
                    this._email = value;
                    this.SendPropertyChanged("email");
                    this.OnemailChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_homePhone", DbType = "VarChar(25)")]
        public string homePhone
        {
            get
            {
                return this._homePhone;
            }
            set
            {
                if ((this._homePhone != value))
                {
                    this.OnhomePhoneChanging(value);
                    this.SendPropertyChanging();
                    this._homePhone = value;
                    this.SendPropertyChanged("homePhone");
                    this.OnhomePhoneChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_mobilePhone", DbType = "VarChar(25)")]
        public string mobilePhone
        {
            get
            {
                return this._mobilePhone;
            }
            set
            {
                if ((this._mobilePhone != value))
                {
                    this.OnmobilePhoneChanging(value);
                    this.SendPropertyChanging();
                    this._mobilePhone = value;
                    this.SendPropertyChanged("mobilePhone");
                    this.OnmobilePhoneChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_additionalPhone", DbType = "VarChar(25)")]
        public string additionalPhone
        {
            get
            {
                return this._additionalPhone;
            }
            set
            {
                if ((this._additionalPhone != value))
                {
                    this.OnadditionalPhoneChanging(value);
                    this.SendPropertyChanging();
                    this._additionalPhone = value;
                    this.SendPropertyChanged("additionalPhone");
                    this.OnadditionalPhoneChanged();
                }
            }
        }

        [AssociationAttribute(Name = "Teacher_TeacherSubject", Storage = "_TeacherSubjects", ThisKey = "idTeacher", OtherKey = "refTeacher")]
        public EntitySet<TeacherSubject> TeacherSubjects
        {
            get
            {
                return this._TeacherSubjects;
            }
            set
            {
                this._TeacherSubjects.Assign(value);
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void AttachTeacherSubjects(TeacherSubject entity)
        {
            this.SendPropertyChanging();
            entity.Teacher = this;
        }

        [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_IsCandidate", DbType = "bit")]
        public System.Nullable<bool> IsCandidate
        {
            get
            {
                return this._IsCandidate;
            }
            set
            {
                if ((this._IsCandidate != value))
                {
                    this.OnIsCandidateChanging(value);
                    this.SendPropertyChanging();
                    this._IsCandidate = value;
                    this.SendPropertyChanged("IsCandidate");
                    this.OnIsCandidateChanged();
                }
            }
        }

        private void DetachTeacherSubjects(TeacherSubject entity)
        {
            this.SendPropertyChanging();
            entity.Teacher = null;
        }

        public override AbstractModel PureModel
        {
            get { return _pureModel; }
            set
            {
                if(_pureModel != null)
                {
                    _pureModel.UpdateModelEvent -= UpdateModelEventHandler;
                    ((ITeacher)_pureModel).Subjects.AddedItemEvent -= AddedSubjectEventHandler;
                    ((ITeacher)_pureModel).Subjects.RemovedItemEvent -= RemovedSubjectEventHandler;
                }

                if (value != null)
                {
                    value.UpdateModelEvent += UpdateModelEventHandler;
                    ((ITeacher)value).Subjects.AddedItemEvent += AddedSubjectEventHandler;
                    ((ITeacher)value).Subjects.RemovedItemEvent += RemovedSubjectEventHandler;
                }

                _pureModel = value;
            }
        }

        private void RemovedSubjectEventHandler(object sender, ModelItemEventArgs e)
        {
            RemovedSubjectEvent(sender, e);
        }

        private void AddedSubjectEventHandler(object sender, ModelItemEventArgs e)
        {
            AddedSubjectEvent(sender, e);
        }

        private void UpdateModelEventHandler(object sender, EventArgs e)
        {
            //  <set new values>
            familyName = ((ITeacher)_pureModel).FamilyName;
            teacherName = ((ITeacher)_pureModel).Name;
            fatherName = ((ITeacher)_pureModel).FatherName;
            bornDate = ((ITeacher)_pureModel).Birthday;
            IsCandidate = ((ITeacher)_pureModel).IsCandidate;
            homePhone =
                ((ITeacher)_pureModel).ContactInformation.LandlinePhone;
            mobilePhone =
                ((ITeacher)_pureModel).ContactInformation.MobilePhone;
            additionalPhone =
                ((ITeacher)_pureModel).ContactInformation.AdditionalPhone;
            email = ((ITeacher)_pureModel).ContactInformation.Email;
            //  </set new values>

            UpdateData();
        }
    }
}